package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Resource
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @Cacheable(value = "setmealCache" , key = "#setmealDTO.categoryId")
    public Result insertSetMeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.insertSetMeal(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result selectSetmeal(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult = setmealService.selectSetmeal(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping()
    public Result deleteSetmeal(@RequestParam List<Long> ids){
        setmealService.deleteSetmeal(ids);
        return Result.success();
    }

    /**
     * 根据ID查询套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectSetmealById(@PathVariable Long id){
        SetmealVO setmealVO = setmealService.selectSetmealById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐信息
     * @param setmealVO
     * @return
     */
    @PutMapping()
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result updateSetMeal(@RequestBody SetmealVO setmealVO){
        setmealService.updateSetMeal(setmealVO);
        return Result.success();
    }

    /**
     * 起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result updateStatus(@PathVariable Integer status , Long id){
        setmealService.updateStatus(status,id);
        return Result.success();
    }
}
